#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node *next;
} SLTNode;

SLTNode *g_node;

SLTNode *SLTFind(SLTNode *phead, int x)
{
    SLTNode *pcur = phead;
    while (pcur != NULL)
    {
        if (pcur->data == x)
        {
            printf(">>>>>>>i: %d\n",x);
            return pcur;
        }
        pcur = pcur->next;
    }
    // pcur == NULL
    return NULL;
}

SLTNode *SLTBuyNode(int x)
{
    SLTNode *newnode = (SLTNode *)malloc(sizeof(SLTNode));
    if (newnode == NULL)
    {
        perror("malloc fail!");
        exit(1);
    }
    newnode->data = x;
    newnode->next = NULL;

    return newnode;
}

// 尾插
void SLTPushBack(SLTNode **pphead, int x)
{
    assert(pphead);
    //*pphead 就是指向第一个节点的指针
    // 空链表和非空链表
    SLTNode *newnode = SLTBuyNode(x);
    if (*pphead == NULL)
    {
        *pphead = newnode;
    }
    else
    {
        // 找尾
        SLTNode *ptail = *pphead;
        while (ptail->next)
        {
            ptail = ptail->next;
        }
        // ptail指向的就是尾结点
        ptail->next = newnode;
    }
}

int main(void)
{
    int i;

    for (i = 0; i < 10; i++)
    {
        if (SLTFind(g_node, i) == NULL)
        {
            SLTPushBack(&g_node,i);
            SLTFind(g_node, i);
        }
    }

    return 0;
}
